package crp.core.course.domain.services;

import crp.core.academic.domain.repositories.AcademicTermRepository;
import crp.core.course.domain.models.CourseAttributesEntity;
import crp.core.course.domain.models.CourseEntity;
import crp.core.course.domain.models.CourseSourceType;
import crp.core.course.domain.models.PlanCourseEntity;
import crp.core.course.domain.repositories.CourseAttributesRepository;
import crp.core.course.domain.repositories.CourseRepository;
import crp.core.course.domain.repositories.PlanCourseRepository;
import crp.core.department.domain.models.DepartmentEntity;
import crp.core.shared.PublicStatus;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class CourseEditor {
    private final CourseRepository courseRepository;
    private final CourseAttributesRepository attributesRepository;
    private final PlanCourseRepository plancourseRepository;
    private final AcademicTermRepository academicTermRepository;

    @Inject
    public CourseEditor(CourseRepository courseRepository,
                        CourseAttributesRepository attributesRepository,
                        PlanCourseRepository plancourseRepository,
                        AcademicTermRepository academicTermRepository) {
        this.courseRepository = courseRepository;
        this.attributesRepository = attributesRepository;
        this.plancourseRepository = plancourseRepository;
        this.academicTermRepository = academicTermRepository;
    }

    public CourseEntity upsert(String code, String name, String openDepartmentId) {
        return courseRepository.findByCode(code)
                .orElseGet(() -> {
                    CourseEntity course = new CourseEntity();
                    course.setCode(code);
                    course.setName(name);
                    course.setTeachingClassCode(code);
                    DepartmentEntity department = new DepartmentEntity();
                    department.setId(openDepartmentId);
                    CourseAttributesEntity attributes = new CourseAttributesEntity();
                    attributes.setOpenDepartment(department);
                    attributes.setAcademicTerm(academicTermRepository.findCurrentTerm());
                    attributes.setSourceType(CourseSourceType.CREATED);
                    attributes.setPublicStatus(PublicStatus.PRIVATE);
                    attributes.setCourse(course);
                    PlanCourseEntity planCourse = new PlanCourseEntity();
                    planCourse.setCode(code);
                    planCourse.setName(name);
                    course.setPlanCourse(planCourse);
                    course.setAttributes(attributes);
                    plancourseRepository.persistAndFlush(planCourse);
                    courseRepository.persist(course);
                    attributesRepository.persist(attributes);
                    return course;
                });
    }

    public void publish(CourseEntity course) {
        course.getAttributes().setPublicStatus(PublicStatus.PUBLIC);
        courseRepository.persist(course);
    }
}
